{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Q# notebooks #\n",
    "\n",
    "This notebook will show you how to use IQ# to write your own quantum application notebooks.\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Compiling Q# operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "An operation is the basic unit of quantum execution in Q#. It is roughly equivalent to a function in C or C++ or Python, or a static method in C# or Java.\n",
    "\n",
    "IQ#, the Q# Jupyter kernel, allows you to write Q# operations directly on a code cell and compile them by running the cell (pressing  Ctrl/⌘+Enter). For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "[\"SayHello\"]",
      "text/html": [
       "<ul><li>SayHello</li></ul>"
      ],
      "text/plain": [
       "SayHello"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "operation SayHello(name: String) : Unit {\n",
    "\n",
    "    // The following line will simply write a message to the console:\n",
    "    Message($\"Hello {name}!\");\n",
    "\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "When you **Run** the cell, Q# compiles the code and returns the name of the operations it found. \n",
    "In this case, it found only one operation (`HelloQ`). \n",
    "\n",
    "If the compiler detects any errors, it will instead show the list of errors in the output. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "C:/snippet_.qs(5,5): error QS5022: No identifier with the name \"FooBar\" exists.\n",
      "C:/snippet_.qs(9,12): error QS6211: Unexpected argument tuple. Expecting an argument of type String.\n"
     ]
    }
   ],
   "source": [
    "operation InvalidQ() : Unit {\n",
    "\n",
    "    // The `FooBar` operation doesn't exist, so the following line\n",
    "    // will generate a `No variable with that name exists.` error:\n",
    "    FooBar(\"Hello again!\");\n",
    "    \n",
    "    // `Message` takes only one string argument, so the following line\n",
    "    // will generate a `Unexpected argument tuple.` error:\n",
    "    Message(1, 2);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Q# operations can call other operations previously defined; they can also call all the operations defined in the \n",
    "[Microsoft.Quantum.Intrinsic](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic) and \n",
    "[Microsoft.Quantum.Canon](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.canon) namespaces (like [Message](https://docs.microsoft.com/qsharp/api/qsharp/microsoft.quantum.intrinsic.message)).\n",
    "\n",
    "For example, you can create a new operation that calls the previously compiled `HelloQ`:\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "[\"HelloAndres\"]",
      "text/html": [
       "<ul><li>HelloAndres</li></ul>"
      ],
      "text/plain": [
       "HelloAndres"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "operation HelloAndres() : Unit {\n",
    "    SayHello(\"Andres\");\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "They can also use all [Q# standard library](https://docs.microsoft.com/qsharp/api/) operations defined in other namespaces by importing the namespace using the `open` statement. For example, to use [`PI`](https://docs.microsoft.com/qsharp/api/prelude/microsoft.quantum.math.pi) you would need to open the ` Microsoft.Quantum.Math` namespace; optionally you can call the operation providing its fully qualified name, for example:\n",
    " "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "[\"HelloPi\"]",
      "text/html": [
       "<ul><li>HelloPi</li></ul>"
      ],
      "text/plain": [
       "HelloPi"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "open Microsoft.Quantum.Math;\n",
    "\n",
    "operation HelloPi() : Unit {\n",
    "    let pi = Microsoft.Quantum.Convert.DoubleAsString(PI());\n",
    "    SayHello(pi);\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You can define multiple operations in a single cell and use any valid Q# code, for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "[\"SetPlus\",\"SetMinus\",\"PrepareRandomMessage\"]",
      "text/html": [
       "<ul><li>SetPlus</li><li>SetMinus</li><li>PrepareRandomMessage</li></ul>"
      ],
      "text/plain": [
       "SetPlus, SetMinus, PrepareRandomMessage"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "open Microsoft.Quantum.Random;\n",
    "\n",
    "/// # Summary\n",
    "/// Sets the qubit's state to |+⟩\n",
    "operation SetPlus(q: Qubit) : Unit {\n",
    "    Reset(q);\n",
    "    H(q);\n",
    "}\n",
    "\n",
    "/// # Summary\n",
    "/// Sets the qubit's state to |-⟩\n",
    "operation SetMinus(q: Qubit) : Unit {\n",
    "    Reset(q);\n",
    "    X(q);\n",
    "    H(q);\n",
    "}\n",
    "\n",
    "/// # Summary\n",
    "/// Randomly prepares the qubit into |+⟩ or |-⟩\n",
    "operation PrepareRandomMessage(q: Qubit) : Unit {\n",
    "\n",
    "    let choice = DrawRandomInt(0, 1);\n",
    "\n",
    "    if (choice == 0) {\n",
    "        Message(\"Prepared |-⟩\");\n",
    "        SetMinus(q);\n",
    "    } else {\n",
    "        Message(\"Prepared |+⟩\");\n",
    "        SetPlus(q);\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "and create other operations that uses them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "[\"NextRandomBit\",\"TestPrepareQubits\"]",
      "text/html": [
       "<ul><li>NextRandomBit</li><li>TestPrepareQubits</li></ul>"
      ],
      "text/plain": [
       "NextRandomBit, TestPrepareQubits"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "open Microsoft.Quantum.Diagnostics;\n",
    "open Microsoft.Quantum.Measurement;\n",
    "\n",
    "operation NextRandomBit() : Result {\n",
    "    using (q = Qubit()) {\n",
    "        SetPlus(q);\n",
    "        return MResetZ(q);\n",
    "    }\n",
    "}\n",
    "\n",
    "operation TestPrepareQubits() : Result {\n",
    "    mutable r = Zero;\n",
    "    \n",
    "    using (qubits = Qubit[5]) {\n",
    "        ApplyToEach(PrepareRandomMessage, qubits);\n",
    "        DumpMachine();\n",
    "        \n",
    "        set r = Measure([PauliX, PauliX, PauliX, PauliX, PauliX], qubits);\n",
    "        \n",
    "        ResetAll(qubits);\n",
    "    }\n",
    "    \n",
    "    return r;\n",
    "}"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Simulating Q# operations\n",
    "\n",
    "\n",
    "Once a Q# operation has been successfully compiled, you can use the `%simulate` command to simulate it. For example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello 3.141592653589793!\r\n"
     ]
    },
    {
     "data": {
      "application/json": "{\"@type\":\"tuple\"}",
      "text/plain": [
       "()"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%simulate HelloPi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "1",
      "text/plain": [
       "One"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%simulate NextRandomBit"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`%simulate` will print any console output on the notebook, and it will return the operation's return value. If the operation returns `Unit` it prints `()`, otherwise it prints the actual value.\n",
    "\n",
    "`%simulate` only accepts operations that take no arguments. If you want to call an operation that accepts parameters, like `HelloQ`, create a wrapper operation that calls it with the corresponding values, like `HelloPi`.\n",
    "\n",
    "As mentioned, all messages are printed on the notebook; this includes calls to [`DumpMachine`](https://docs.microsoft.com/en-us/qsharp/api/prelude/microsoft.quantum.extensions.diagnostics.dumpmachine):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "\"\\\"bitstring\\\"\"",
      "text/plain": [
       "\"bitstring\""
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%config dump.basisStateLabelingConvention = \"bitstring\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Prepared |-⟩\n",
      "Prepared |+⟩\n",
      "Prepared |+⟩\n",
      "Prepared |-⟩\n",
      "Prepared |+⟩\n"
     ]
    },
    {
     "data": {
      "application/json": "{\"QubitIds\":[0,1,2,3,4],\"NQubits\":5,\"Amplitudes\":[{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0},{\"Real\":-0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":3.141592653589793},{\"Real\":0.1767766952966369,\"Imaginary\":0.0,\"Magnitude\":0.1767766952966369,\"Phase\":0.0}]}",
      "text/html": [
       "\r\n",
       "                    <table style=\"table-layout: fixed; width: 100%\">\r\n",
       "                        <thead>\r\n",
       "                            \r\n",
       "                        <tr>\r\n",
       "                            <th>Qubit IDs</th>\r\n",
       "                            <td span=\"3\">0, 1, 2, 3, 4</td>\r\n",
       "                        </tr>\r\n",
       "                    \r\n",
       "                            <tr>\r\n",
       "                                <th style=\"width: 20ch)\">Basis state (bitstring)</th>\r\n",
       "                                <th style=\"width: 20ch\">Amplitude</th>\r\n",
       "                                <th style=\"width: calc(100% - 26ch - 20ch)\">Meas. Pr.</th>\r\n",
       "                                <th style=\"width: 6ch\">Phase</th>\r\n",
       "                            </tr>\r\n",
       "                        </thead>\r\n",
       "\r\n",
       "                        <tbody>\r\n",
       "                            \r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|00000\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|00001\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|00010\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|00011\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|00100\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|00101\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|00110\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|00111\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|01000\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|01001\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|01010\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|01011\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|01100\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|01101\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|01110\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|01111\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|10000\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|10001\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|10010\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|10011\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|10100\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|10101\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|10110\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|10111\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|11000\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|11001\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|11010\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|11011\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|11100\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|11101\\right\\rangle$</td>\r\n",
       "                                <td>$-0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(180deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|11110\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \n",
       "\r\n",
       "                            <tr>\r\n",
       "                                <td>$\\left|11111\\right\\rangle$</td>\r\n",
       "                                <td>$0.1768 + 0.0000 i$</td>\r\n",
       "                                <td>\r\n",
       "                                    <progress\r\n",
       "                                        max=\"100\"\r\n",
       "                                        value=\"3.125000000000001\"\r\n",
       "                                        style=\"width: 100%;\"\r\n",
       "                                    >\r\n",
       "                                </td>\r\n",
       "                                \r\n",
       "                                <td style=\"transform: rotate(0deg);\r\n",
       "                   text-align: center;\">\r\n",
       "                                 ↑\r\n",
       "                                </td>\r\n",
       "                            \r\n",
       "                            </tr>\r\n",
       "                        \r\n",
       "                        </tbody>\r\n",
       "                    </table>\r\n",
       "                "
      ],
      "text/plain": [
       "|00000⟩\t0.1767766952966369 + 0𝑖\n",
       "|00001⟩\t0.1767766952966369 + 0𝑖\n",
       "|00010⟩\t-0.1767766952966369 + 0𝑖\n",
       "|00011⟩\t-0.1767766952966369 + 0𝑖\n",
       "|00100⟩\t0.1767766952966369 + 0𝑖\n",
       "|00101⟩\t0.1767766952966369 + 0𝑖\n",
       "|00110⟩\t-0.1767766952966369 + 0𝑖\n",
       "|00111⟩\t-0.1767766952966369 + 0𝑖\n",
       "|01000⟩\t0.1767766952966369 + 0𝑖\n",
       "|01001⟩\t0.1767766952966369 + 0𝑖\n",
       "|01010⟩\t-0.1767766952966369 + 0𝑖\n",
       "|01011⟩\t-0.1767766952966369 + 0𝑖\n",
       "|01100⟩\t0.1767766952966369 + 0𝑖\n",
       "|01101⟩\t0.1767766952966369 + 0𝑖\n",
       "|01110⟩\t-0.1767766952966369 + 0𝑖\n",
       "|01111⟩\t-0.1767766952966369 + 0𝑖\n",
       "|10000⟩\t-0.1767766952966369 + 0𝑖\n",
       "|10001⟩\t-0.1767766952966369 + 0𝑖\n",
       "|10010⟩\t0.1767766952966369 + 0𝑖\n",
       "|10011⟩\t0.1767766952966369 + 0𝑖\n",
       "|10100⟩\t-0.1767766952966369 + 0𝑖\n",
       "|10101⟩\t-0.1767766952966369 + 0𝑖\n",
       "|10110⟩\t0.1767766952966369 + 0𝑖\n",
       "|10111⟩\t0.1767766952966369 + 0𝑖\n",
       "|11000⟩\t-0.1767766952966369 + 0𝑖\n",
       "|11001⟩\t-0.1767766952966369 + 0𝑖\n",
       "|11010⟩\t0.1767766952966369 + 0𝑖\n",
       "|11011⟩\t0.1767766952966369 + 0𝑖\n",
       "|11100⟩\t-0.1767766952966369 + 0𝑖\n",
       "|11101⟩\t-0.1767766952966369 + 0𝑖\n",
       "|11110⟩\t0.1767766952966369 + 0𝑖\n",
       "|11111⟩\t0.1767766952966369 + 0𝑖"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/json": "0",
      "text/plain": [
       "Zero"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%simulate TestPrepareQubits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Estimating resources\n",
    "\n",
    "The `%estimate` command lets you estimate the resources a given quantum operation will need to execute, without actually executing the operation. Similar to `%simulate` it takes the name of a no-arguments operation. However, `%estimate` does not keep track of the qubit's state and will not return the output of the operation, instead it returns the estimated values of how many resources, like Qubits and CNOT gates, the corresponding operation will use:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "[{\"Metric\":\"CNOT\",\"Sum\":0.0,\"Max\":0.0},{\"Metric\":\"QubitClifford\",\"Sum\":9.0,\"Max\":9.0},{\"Metric\":\"R\",\"Sum\":0.0,\"Max\":0.0},{\"Metric\":\"Measure\",\"Sum\":11.0,\"Max\":11.0},{\"Metric\":\"T\",\"Sum\":0.0,\"Max\":0.0},{\"Metric\":\"Depth\",\"Sum\":0.0,\"Max\":0.0},{\"Metric\":\"Width\",\"Sum\":5.0,\"Max\":5.0},{\"Metric\":\"BorrowedWidth\",\"Sum\":0.0,\"Max\":0.0}]",
      "text/html": [
       "<table><thead><tr><th style=\"text-align: start;\">Metric</th><th style=\"text-align: start;\">Sum</th><th style=\"text-align: start;\">Max</th></tr></thead><tbody><tr><td style=\"text-align: start;\">CNOT</td><td style=\"text-align: start;\">0</td><td style=\"text-align: start;\">0</td></tr><tr><td style=\"text-align: start;\">QubitClifford</td><td style=\"text-align: start;\">9</td><td style=\"text-align: start;\">9</td></tr><tr><td style=\"text-align: start;\">R</td><td style=\"text-align: start;\">0</td><td style=\"text-align: start;\">0</td></tr><tr><td style=\"text-align: start;\">Measure</td><td style=\"text-align: start;\">11</td><td style=\"text-align: start;\">11</td></tr><tr><td style=\"text-align: start;\">T</td><td style=\"text-align: start;\">0</td><td style=\"text-align: start;\">0</td></tr><tr><td style=\"text-align: start;\">Depth</td><td style=\"text-align: start;\">0</td><td style=\"text-align: start;\">0</td></tr><tr><td style=\"text-align: start;\">Width</td><td style=\"text-align: start;\">5</td><td style=\"text-align: start;\">5</td></tr><tr><td style=\"text-align: start;\">BorrowedWidth</td><td style=\"text-align: start;\">0</td><td style=\"text-align: start;\">0</td></tr></tbody></table>"
      ],
      "text/plain": [
       "Metric        Sum Max\r\n",
       "------------- -- --\r\n",
       "CNOT          0  0\r\n",
       "QubitClifford 9  9\r\n",
       "R             0  0\r\n",
       "Measure       11 11\r\n",
       "T             0  0\r\n",
       "Depth         0  0\r\n",
       "Width         5  5\r\n",
       "BorrowedWidth 0  0\r\n"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%estimate TestPrepareQubits"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To learn more about resources estimation, take a look at [The ResourcesEstimator Target Machine](https://docs.microsoft.com/en-us/quantum/machines/resources-estimator?) documentation."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## The Workspace\n",
    "\n",
    "The notebook uses the folder it lives on disk to define a workspace. It will try to compile all the Q# files (i.e. all files with a `.qs` extension) it finds under the current folder and will make the operations it finds available to operations in the notebook. For example, the [Operations.qs](/Operations.qs) file in this folder defines two operations:\n",
    "* Microsoft.Quantum.Samples.IsMinus\n",
    "* Microsoft.Quantum.Samples.IsPlus\n",
    "\n",
    "To get the list of operations defined in the workspace, you can use the `%workspace` command:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "[\"Microsoft.Quantum.Samples.IsMinus\",\"Microsoft.Quantum.Samples.IsPlus\"]",
      "text/html": [
       "<ul><li>Microsoft.Quantum.Samples.IsMinus</li><li>Microsoft.Quantum.Samples.IsPlus</li></ul>"
      ],
      "text/plain": [
       "Microsoft.Quantum.Samples.IsMinus, Microsoft.Quantum.Samples.IsPlus"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%workspace"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "These operations can be used in this notebook, for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "[\"CheckPlus\"]",
      "text/html": [
       "<ul><li>CheckPlus</li></ul>"
      ],
      "text/plain": [
       "CheckPlus"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "open Microsoft.Quantum.Samples;\n",
    "\n",
    "operation CheckPlus() : Bool {\n",
    "    mutable result = false;\n",
    "    \n",
    "    using (q = Qubit()) {\n",
    "        SetPlus(q);\n",
    "        set result = IsPlus(q);\n",
    "        \n",
    "        Reset(q);\n",
    "    }\n",
    "\n",
    "    return result;\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "true",
      "text/plain": [
       "True"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%simulate CheckPlus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "To pick up any changes you make to a Q# file in the workspace, use `%workspace reload`. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "{\"LastUpdated\":\"2020-09-02T06:59:53.1427237-07:00\",\"IsCompleted\":true,\"Description\":\"Reloading workspace\",\"Subtask\":\"done\"}",
      "text/plain": [
       "Reloading workspace: done!"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/json": "[\"Microsoft.Quantum.Samples.IsMinus\",\"Microsoft.Quantum.Samples.IsPlus\"]",
      "text/html": [
       "<ul><li>Microsoft.Quantum.Samples.IsMinus</li><li>Microsoft.Quantum.Samples.IsPlus</li></ul>"
      ],
      "text/plain": [
       "Microsoft.Quantum.Samples.IsMinus, Microsoft.Quantum.Samples.IsPlus"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%workspace reload"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Getting Help ##\n",
    "\n",
    "Q# supports adding documentation to operations via comments in the code. When such documentation exists, you can access it from the notebook by adding a question mark before or after the operation name on a code cell, for example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "{\"name\":\"Microsoft.Quantum.Intrinsic.X\",\"kind\":3,\"source\":\"C:\\\\Users\\\\ryansha\\\\.dotnet\\\\tools\\\\.store\\\\microsoft.quantum.iqsharp\\\\0.12.20082513\\\\microsoft.quantum.iqsharp\\\\0.12.20082513\\\\tools\\\\netcoreapp3.1\\\\any\\\\Microsoft.Quantum.QSharp.Core.dll\",\"documentation\":\" # Summary\\n Applies the Pauli $X$ gate.\\n\\n \\\\begin{align}\\n     \\\\sigma_x \\\\mathrel{:=}\\n     \\\\begin{bmatrix}\\n         0 & 1 \\\\\\\\\\\\\\\\\\n         1 & 0\\n     \\\\end{bmatrix}.\\n \\\\end{align}\\n\\n # Input\\n ## qubit\\n Qubit to which the gate should be applied.\"}",
      "text/html": [
       "<h4><i class=\"fa fas fa-terminal\"></i> Microsoft.Quantum.Intrinsic.X <a href=\"C:\\Users\\ryansha\\.dotnet\\tools\\.store\\microsoft.quantum.iqsharp\\0.12.20082513\\microsoft.quantum.iqsharp\\0.12.20082513\\tools\\netcoreapp3.1\\any\\Microsoft.Quantum.QSharp.Core.dll\"><i class=\"fa fas fa-code\"></i></a></h4><h1>Summary</h1>\n",
       "<p>Applies the Pauli $X$ gate.</p>\n",
       "<p>\\begin{align}\n",
       "\\sigma_x \\mathrel{:=}\n",
       "\\begin{bmatrix}\n",
       "0 &amp; 1 \\\\\n",
       "1 &amp; 0\n",
       "\\end{bmatrix}.\n",
       "\\end{align}</p>\n",
       "<h1>Input</h1>\n",
       "<h2>qubit</h2>\n",
       "<p>Qubit to which the gate should be applied.</p>\n"
      ],
      "text/plain": [
       "Microsoft.Quantum.Intrinsic.X"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Microsoft.Quantum.Intrinsic.X?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This documentation is available for any operations in the Prelude, Canon and Workspace, or even those defined locally in the notebook:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "{\"name\":\"PrepareRandomMessage\",\"kind\":3,\"source\":\"C:\\\\QDK\\\\quantum\\\\samples\\\\getting-started\\\\intro-to-iqsharp\\\\obj\\\\__snippets__.dll\",\"documentation\":\" # Summary\\n Randomly prepares the qubit into |+⟩ or |-⟩\"}",
      "text/html": [
       "<h4><i class=\"fa fas fa-terminal\"></i> PrepareRandomMessage <a href=\"C:\\QDK\\quantum\\samples\\getting-started\\intro-to-iqsharp\\obj\\__snippets__.dll\"><i class=\"fa fas fa-code\"></i></a></h4><h1>Summary</h1>\n",
       "<p>Randomly prepares the qubit into |+⟩ or |-⟩</p>\n"
      ],
      "text/plain": [
       "PrepareRandomMessage"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "PrepareRandomMessage?"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Other commands ##"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `%who`\n",
    "\n",
    "`%who` returns the list of all local and workspace operations available."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "[\"CheckPlus\",\"HelloAndres\",\"HelloPi\",\"Microsoft.Quantum.Samples.IsMinus\",\"Microsoft.Quantum.Samples.IsPlus\",\"NextRandomBit\",\"PrepareRandomMessage\",\"SayHello\",\"SetMinus\",\"SetPlus\",\"TestPrepareQubits\"]",
      "text/html": [
       "<ul><li>CheckPlus</li><li>HelloAndres</li><li>HelloPi</li><li>Microsoft.Quantum.Samples.IsMinus</li><li>Microsoft.Quantum.Samples.IsPlus</li><li>NextRandomBit</li><li>PrepareRandomMessage</li><li>SayHello</li><li>SetMinus</li><li>SetPlus</li><li>TestPrepareQubits</li></ul>"
      ],
      "text/plain": [
       "CheckPlus, HelloAndres, HelloPi, Microsoft.Quantum.Samples.IsMinus, Microsoft.Quantum.Samples.IsPlus, NextRandomBit, PrepareRandomMessage, SayHello, SetMinus, SetPlus, TestPrepareQubits"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%who"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `%package`\n",
    "\n",
    "`%package` allows you to load nuget packages and makes available any Q# operations defined on them. For example, to use the operations from [Q#'s Quantum Chemistry Library](https://docs.microsoft.com/en-us/quantum/libraries/chemistry/?view=qsharp-preview), you must load the [Microsoft.Quantum.Chemistry](https://www.nuget.org/packages/Microsoft.Quantum.Chemistry/) nuget package:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "{\"LastUpdated\":\"2020-09-02T06:59:55.9798634-07:00\",\"IsCompleted\":true,\"Description\":\"Adding package Microsoft.Quantum.Chemistry\",\"Subtask\":\"done\"}",
      "text/plain": [
       "Adding package Microsoft.Quantum.Chemistry: done!"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/json": "[\"Microsoft.Quantum.Standard::0.12.20082513\",\"Microsoft.Quantum.Chemistry::0.12.20082513\"]",
      "text/html": [
       "<ul><li>Microsoft.Quantum.Standard::0.12.20082513</li><li>Microsoft.Quantum.Chemistry::0.12.20082513</li></ul>"
      ],
      "text/plain": [
       "Microsoft.Quantum.Standard::0.12.20082513, Microsoft.Quantum.Chemistry::0.12.20082513"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%package Microsoft.Quantum.Chemistry"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`%package` returns the list of nuget packages currently loaded and their version."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `%project`\n",
    "\n",
    "`%project` allows you to load Q# projects from folders outside the current workspace and makes available any Q# operations defined in them. For example, to use the operations from the [quantum random number generator sample](https://github.com/microsoft/Quantum/tree/rmshaffer/project-reference-samples/samples/getting-started/qrng), you may add a reference to the `.csproj` file in that folder:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Adding reference to project: ../qrng/Qrng.csproj\r\n"
     ]
    },
    {
     "data": {
      "application/json": "{\"LastUpdated\":\"2020-09-02T06:59:59.2273749-07:00\",\"IsCompleted\":true,\"Description\":\"Reloading workspace\",\"Subtask\":\"done\"}",
      "text/plain": [
       "Reloading workspace: done!"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "data": {
      "application/json": "[\"C:\\\\QDK\\\\quantum\\\\samples\\\\getting-started\\\\qrng\\\\Qrng.csproj\"]",
      "text/html": [
       "<ul><li>C:\\QDK\\quantum\\samples\\getting-started\\qrng\\Qrng.csproj</li></ul>"
      ],
      "text/plain": [
       "C:\\QDK\\quantum\\samples\\getting-started\\qrng\\Qrng.csproj"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "%project ../qrng/Qrng.csproj"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "`%project` returns the list of Q# projects currently loaded."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### `%version`\n",
    "\n",
    "`%version` simply returns the current versions of IQ# and of Jupyter Core (a library used by IQ#):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "application/json": "{\"rows\":[{\"@type\":\"@tuple\",\"Item1\":\"iqsharp\",\"Item2\":\"0.12.20082513\"},{\"@type\":\"@tuple\",\"Item1\":\"Jupyter Core\",\"Item2\":\"1.4.0.0\"},{\"@type\":\"@tuple\",\"Item1\":\".NET Runtime\",\"Item2\":\".NETCoreApp,Version=v3.1\"}]}",
      "text/html": [
       "<table><thead><tr><th style=\"text-align: start;\">Component</th><th style=\"text-align: start;\">Version</th></tr></thead><tbody><tr><td style=\"text-align: start;\">iqsharp</td><td style=\"text-align: start;\">0.12.20082513</td></tr><tr><td style=\"text-align: start;\">Jupyter Core</td><td style=\"text-align: start;\">1.4.0.0</td></tr><tr><td style=\"text-align: start;\">.NET Runtime</td><td style=\"text-align: start;\">.NETCoreApp,Version=v3.1</td></tr></tbody></table>"
      ],
      "text/plain": [
       "Component    Version\r\n",
       "------------ ------------------------\r\n",
       "iqsharp      0.12.20082513\r\n",
       "Jupyter Core 1.4.0.0\r\n",
       ".NET Runtime .NETCoreApp,Version=v3.1\r\n"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "%version"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Q#",
   "language": "qsharp",
   "name": "iqsharp"
  },
  "language_info": {
   "file_extension": ".qs",
   "mimetype": "text/x-qsharp",
   "name": "qsharp",
   "version": "0.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
